package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.JiaoFuJiHuaService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jfjh")
public class JiaoFuJiHuaController {
    @Autowired
    JiaoFuJiHuaService jiaoFuJiHuaService;

    @RequestMapping("/addJiaoFuJiHua")
    @ResponseBody
    //新建交付计划
    public void addJiaoFuJiHua(@RequestBody Jiaofujihua jiaofujihua){
       jiaoFuJiHuaService.addJiaoFuJiHua(jiaofujihua);
    }

    //查看所有交付计划
    @RequestMapping("/ckJiaoFuJiHua")
    @ResponseBody
    public Map<String,Object> bB(String ddHao, String khName, String time,
                                 Integer pageNumm, Integer pageSizee, HttpSession session){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);

        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            //日期这里会有问题，如果是月底的话就会出错，要判断一下是大月还是小月，是否是闰年
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            System.err.println("tim2:"+time2);
            time3 = time2.toString();
        }

        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }

        List<Jiaofujihua> list = jiaoFuJiHuaService.selectAllJiaoFuJiHua(ddHao,khName,time3,empName);

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }

    //移除交付计划
    @RequestMapping("/yichuJiaoFuJiHua")
    @ResponseBody
    public String yichuJiaoFuJiHua(Integer jjId){

        return jiaoFuJiHuaService.yichuJiaoFuJiHua(jjId);
    }

    //根据交计id找到交计详情
    @RequestMapping("/ckJJXQbyjjId")
    @ResponseBody
    public List<Jiaofujihuaxiangqing> ckJJXQbyjjId(Integer jjId){
        return jiaoFuJiHuaService.ckJJXQbyjjId(jjId);
    }

    //根据交付计划id查看交付计划
    @RequestMapping("/selectJiaoFuJiHuaByjjId")
    @ResponseBody
    public Jiaofujihua selectJiaoFuJiHuaByjjId(Integer jjId){
        return jiaoFuJiHuaService.selectJiaoFuJiHuaByjjId(jjId);
    }
}
